<?php
# ============================================================================
# 版权所有  Chilcoo.com，并保留所有权利。
# 网站地址: http://www.chilcoo.com/erphp
# ----------------------------------------------------------------------------
# 这不是一个自由软件！你不能通过任何方式破解本软件.
# 使用:不允许对程序代码以任何形式任何目的的再发布。
# ============================================================================
# $Author: Chilcoo Inc. *
# $Official website : www.chilcoo.com
# 
# 文件信息:
# File : /temp/default.php
# 分类列表页面

# 载入常用集
# 导入common
require_once (dirname(__FILE__).'/../../include/common.inc.php');
require_once (dirname(__FILE__).'/../../include/_manager/login.inc.php');
# 页面级别
$conf_web_page_level = 0;

class page_index extends page{
	
	function __construct(){
		$this->database = new db(config::conf_db_host,config::conf_db_user,
			config::conf_db_password,config::conf_db_name);
	}
	function lang_listview(){
		$lv = null;
		try{
			$sql = "SELECT lang,name 
				FROM cs2_langset ORDER BY lang ASC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			
		}
		return $lv;
	}
	function prod_listview(){
		$lv = null;
		try{
			$sql = "SELECT prodId,name,model 
				FROM cs2_prod ORDER BY model ASC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			
		}
		return $lv;
	}
	function len(){
		global $_login;
		$i = 0;
		try{
			$sql = "SELECT COUNT(*) AS cnt FROM cs2_temp WHERE (adminId = ".$_login->adminId.");";
			$row = $this->database->get_one($sql);
			$i = $row['cnt'];
		}catch (Exception $e) {
			
		}
		return $i;
	}
	function listview(){
		global $_login;
		$lv = null;
		try{
			$sql = "SELECT tempId,cs2_temp.title,cs2_temp.info,cs2_temp.prodId,
				cs2_temp.guid,cs2_temp.lang,dates,cs2_temp.adminId,cs2_admin.name AS adminname,
				cs2_prod.name,cs2_prod.model,cs2_langset.fontset,cs2_langset.name AS langname
				FROM cs2_temp
			LEFT JOIN (cs2_langset,cs2_prod,cs2_admin)
			ON (cs2_langset.lang = cs2_temp.lang AND cs2_prod.prodId = cs2_temp.prodId
				AND  cs2_admin.adminId = cs2_temp.adminId)
			WHERE (cs2_temp.adminId = ".$_login->adminId.")
			ORDER BY prodId ASC;";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			
		}
		return $lv;
	}

	function ajax_callback(){
		global $config;
		global $_login;
		# POST 数据
		$token = $_POST['token'];
		# 初始返回用 json
		$json = array("ref"=>"error");
		if($token == config::TOKEN_ADD_TEMP){
			$lang = $_POST['lang'];
			$prodId = $_POST['prodId'];
			$title = htmlspecialchars($_POST['title']);
			$info = htmlspecialchars($_POST['info']);
			$guid = $config->guid();
			
			$sql = "INSERT INTO cs2_temp (lang, prodId, title, info, guid,dates,adminId ) 
				VALUES (".$lang.",".$prodId.",'".$title."','".$info."','".$guid."',NOW(),".$_login->adminId.");";
			
			try {
				$this->database->insert($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_UP_TEMP){
			$lang = $_POST['lang'];
			$prodId = $_POST['prodId'];
			$title = htmlspecialchars($_POST['title']);
			$info = htmlspecialchars($_POST['info']);
			$tempId = $_POST['tempId'];
			
			
			$sql = "UPDATE cs2_temp SET 
				lang=".$lang." ,
				prodId=".$prodId." ,
				title='".$title."' ,
				info='".$info."' WHERE (tempId =". $tempId.");";

			try {
				$this->database->update($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_VIEW_TEMP){
			$tempId = $_POST['tempId'];
			try{
				$sql = "SELECT info FROM cs2_temp WHERE tempId='".$tempId."';";
				$row = $this->database->get_one($sql);
				$json = array("ref"=>"OK","id"=>$tempId,"view"=>htmlspecialchars_decode($row['info']));
			}catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		if($token == config::TOKEN_DEL_TEMP){
			$tempId = $_POST['tempId'];
			$sql = "DELETE FROM cs2_temp WHERE (tempId=".$tempId.")";
			try {
				$this->database->delete($sql);
				$json = array("ref"=>"OK");
			} catch (Exception $e) {
				$json = array("ref"=>"error,database.");
			}
		}
		return json_encode($json);
	}
}

$pg = new page_index();
# Session 和 POST 安全访问.
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$_SESSION[config::conf_session_id_token] = $config->web_page_period;
	$config->smarty->assign('len', $pg->len());
	$config->smarty->assign('listview', $pg->listview());
	$config->smarty->assign('lang_listview', $pg->lang_listview());
	$config->smarty->assign('prod_listview', $pg->prod_listview());
	# 加载页面
	$pg->render($config->temp_page_path.config::conf_web_template_extension);
}else{
	print_r($pg->ajax_callback());
}

























?>